/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PasswordSetting.java
 *
 * Created on Oct 29, 2011, 5:38:15 PM
 */
package VIMS;

import javax.swing.JOptionPane;

/**
 *
 * @author shyan
 */
public class PasswordSetting extends javax.swing.JFrame {

    private  String OldKey    = null;
    private  String NewKey    = null;
    private  String RetypeKey = null;
    private  String UserName  = null;
    private  Login  eqOB      = null;
    private  Database dbOB    = new Database();
            
    public PasswordSetting() {
        super("Password Setting");
        
        initComponents();
        PwAlertMsg.setVisible(false);
        setLocationRelativeTo(null);
    }
    
    public PasswordSetting(String UserID) {
        super("Password Setting");
        UserName = UserID;
        
        initComponents();
        PwAlertMsg.setVisible(false);
        setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        OldPw = new javax.swing.JPasswordField();
        NewPw = new javax.swing.JPasswordField();
        RetypePw = new javax.swing.JPasswordField();
        OldPwLabel = new javax.swing.JLabel();
        NewPwLabel = new javax.swing.JLabel();
        RNewPwLabel = new javax.swing.JLabel();
        Change = new javax.swing.JButton();
        Cancel = new javax.swing.JButton();
        PwAlertMsg = new javax.swing.JLabel();
        Border = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(400, 300));
        setResizable(false);

        jLayeredPane1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jLayeredPane1.setMinimumSize(new java.awt.Dimension(400, 300));

        OldPw.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OldPwActionPerformed(evt);
            }
        });
        OldPw.setBounds(200, 70, 140, 30);
        jLayeredPane1.add(OldPw, javax.swing.JLayeredPane.DEFAULT_LAYER);
        NewPw.setBounds(200, 110, 140, 30);
        jLayeredPane1.add(NewPw, javax.swing.JLayeredPane.DEFAULT_LAYER);
        RetypePw.setBounds(200, 150, 140, 30);
        jLayeredPane1.add(RetypePw, javax.swing.JLayeredPane.DEFAULT_LAYER);

        OldPwLabel.setText("Old Password    :");
        OldPwLabel.setBounds(60, 70, 100, 30);
        jLayeredPane1.add(OldPwLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        NewPwLabel.setText("New Password  :");
        NewPwLabel.setBounds(60, 110, 100, 30);
        jLayeredPane1.add(NewPwLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        RNewPwLabel.setText("Retype New Password  :");
        RNewPwLabel.setBounds(60, 150, 140, 30);
        jLayeredPane1.add(RNewPwLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Change.setText("Change");
        Change.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ChangeActionPerformed(evt);
            }
        });
        Change.setBounds(190, 210, 80, 30);
        jLayeredPane1.add(Change, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Cancel.setText("Cancel");
        Cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelActionPerformed(evt);
            }
        });
        Cancel.setBounds(280, 210, 80, 30);
        jLayeredPane1.add(Cancel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        PwAlertMsg.setText("Timeout: YOU ARE REQUIRED TO CHANGE YOUR PW ==");
        PwAlertMsg.setBounds(40, 260, 330, 30);
        jLayeredPane1.add(PwAlertMsg, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Border.setBorder(javax.swing.BorderFactory.createTitledBorder("Change Password"));
        Border.setBounds(30, 30, 340, 170);
        jLayeredPane1.add(Border, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void OldPwActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OldPwActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_OldPwActionPerformed

    private void CancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_CancelActionPerformed

    private void ChangeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ChangeActionPerformed
        Check();
    }//GEN-LAST:event_ChangeActionPerformed

    private void Check()
    {
        OldKey       = OldPw.getText().trim();
        NewKey       = NewPw.getText().trim();
        RetypeKey    = RetypePw.getText().trim();
        
        if(OldKey.isEmpty() || NewKey.isEmpty() || RetypeKey.isEmpty())
            JOptionPane.showMessageDialog(null,"Please enter all the field.");
        else
        {    
            eqOB = new Login(UserName,OldKey);
            if(eqOB.check()==true)
            {
              if(NewKey.equals(RetypeKey.toString()))
              {

                  String SQL=  "UPDATE password SET PW_PASSWORD = ( DES_ENCRYPT('"+NewKey+"') ) ,PW_DATE = CURDATE() WHERE PW_NUMBER IN( "
                         + "SELECT PW_NUMBER FROM employee WHERE EMP_USERNAME = '"+UserName+"')";                  
                  //System.out.println(SQL);
                  if(dbOB.update(SQL)==true){
                      JOptionPane.showMessageDialog(null, "Password changed successfully.");
                      this.setVisible(false);
                  }
                  else
                      JOptionPane.showMessageDialog(null, "Failure occured.");
                  
                  OldPw.setText("");
                  NewPw.setText("");
                  RetypePw.setText("");
              }
              else
                  JOptionPane.showMessageDialog(null, "New password not matched.");
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Invalid Old Password.",null, JOptionPane.ERROR_MESSAGE);
            }
        }    
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PasswordSetting.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PasswordSetting.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PasswordSetting.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PasswordSetting.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new PasswordSetting().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Border;
    private javax.swing.JButton Cancel;
    private javax.swing.JButton Change;
    private javax.swing.JPasswordField NewPw;
    private javax.swing.JLabel NewPwLabel;
    private javax.swing.JPasswordField OldPw;
    private javax.swing.JLabel OldPwLabel;
    private javax.swing.JLabel PwAlertMsg;
    private javax.swing.JLabel RNewPwLabel;
    private javax.swing.JPasswordField RetypePw;
    private javax.swing.JLayeredPane jLayeredPane1;
    // End of variables declaration//GEN-END:variables
}
